#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;

require('function.pl');
require('config.pl');

my @domains;
open IN, 'data/spam_domain' or die("fail	open data/spam_domain\n");
while (<IN>) {
	chop;
	push @domains, $_ if (length() > 5);
}
close IN;

my @keywords;
open IN, 'data/keywords' or die("fail	open data/keywords\n");
while (<IN>) {
	chop;
	push @keywords, $_ if (length() > 1);
}
close IN;

my $db_conn = DBI->connect("DBI:mysql:database=phone_num_search;host=$ENV{'db_host'}", $ENV{'db_user'}, $ENV{'db_password'});
$db_conn->do('set names utf8');
$db_conn->{'mysql_use_result'} = 1;
open IN, "data/baidu_wap_spider" or die("fail	open data/baidu_wap_spider\n");
my $sql;
while (<IN>) {
	chomp();
	my ($phone_num, $rank, $site, $title)= split("\t");
	next if (!defined($title));
	my $match = 0;
	foreach my $domain (@domains) {
		my $pos = index($site, $domain);
		if ($pos >= 0) {
			$match = 1;
			last;
		}
	}
	if ($match) {
#		$db_conn->do("delete from phone_num where phone_num = '$phone_num' and rank = $rank");
	}
	else {
		my $flag = 0;
		my $kw_idx = 1;
		foreach my $keyword (@keywords) {
			my $pos = index($title, $keyword);
			if ($pos >= 0) {
				if (!$flag) {
					if (execute_scalar("select count(*) from phone_num where phone_num = '$phone_num' and rank = $rank", $db_conn) == 0) {
						$title = $db_conn->quote($title);
						$sql = "replace into phone_num(phone_num, rank, url, title) values('$phone_num', $rank, '$site', $title)";
						$db_conn->do($sql);
					}
					else {
						$db_conn->do("update phone_num set keyword1 = NULL, keyword2 = NULL, keyword3 = NULL, keyword4 = NULL, keyword5 = NULL where phone_num = '$phone_num' and rank = $rank");
					}
					$flag = 1;
				}
				$sql = "update phone_num set keyword$kw_idx = '$keyword' where phone_num = '$phone_num' and rank = $rank";
				$db_conn->do($sql);
				++$kw_idx;
				last if ($kw_idx >= 6);
#				print "$sql\n";
			}
		}
		if (!$flag) {
#			$db_conn->do("delete from phone_num where phone_num = '$phone_num' and rank = $rank");
		}
	}
}
